// pages/doctor/doctor.js
var app = getApp();
var util = require('../../utils/util');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    currentTab: 0,
    synopsis:'',
    specialty: '',
    id: '',
    department_id:'',
    name: '',
    position: 'keshi',
    image: '',
    jieshou: '',
    status:"",
    upLimit:"",
    keshi: "keshi",
    dates: '2020-10-20',
    iscold: false,
    table: [
      {
        shijain: '上午10:00',
        hao: '24',
        qian: '20.00'
      },
    ],
    //提交添加人员信息
    isSubmit: false,
      phone: "",
      suerName: "",
      isPub: false,
      sex: "男"
      
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this
    var inw = JSON.parse(options.item)
    console.log(inw)
    if(inw.status==1){
      inw.status = "男"
    }else if(inw.status==0){
      inw.status = "女"
    }
    that.setData({
      specialty: inw.specialty,
      id: inw.id,
      department_id:inw.department_id,
      name: inw.name,
      position: inw.position,
      image: inw.image,
      // 主治
      jieshou: inw.specialty,
      // 医生简介
      synopsis:inw.synopsis,
      status:inw.status,
      upLimit:inw.upLimit
    })
    wx.setStorage({
      data: inw.name,
      key: 'name',
    })
    wx.setStorage({
      data: inw.image,
      key: 'image',
    })
    wx.setStorage({
      data: that.dates,
      key: 'dates',
    })
    wx.getStorage({
      key: 'asd',
      success (e) {
        that.setData({
          keshi: e.data
        })
      }

    })
    wx.setStorage({
      data: that.data.dates,
      key: 'dates',
    })
    wx.request({
      url: app.globalData.api+'islike/?doctor_id='+that.data.department_id,
      method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      header: { cookie: app.globalData.header }, // 设置请求的 header
      success: function(res){
       if(res.data == 1){
         that.setData({
          iscold:true
         })
       }
      },
    })
  },
  //滑动切换
  swiperTab: function (e) {
    var that = this;
    that.setData({
      currentTab: e.detail.current
    });
  },
  //点击切换
  clickTab: function (e) {
  
    var that = this;
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current
      })
    }
  },
  //选择时间
  bindDateChange: function (e) {
    // 当前时间
    var nowTime = util.formatTime(new Date())
    // 判断选择时间是否小于当前时间
    if(e.detail.value < nowTime){    
      wx.showToast({
        title: '预约需超过当日！',
      })
      return;
    }else{
      this.setData({
        dates: e.detail.value
      })
      wx.setStorage({
        data: e.detail.value,
        key: 'dates',
      })
    }
   
  },
  //跳转确认挂号
  confirm (e) {
    var item = JSON.stringify(e.currentTarget.dataset.item)
    wx.navigateTo({
      url: '../confirm/confirm?item=' + item,
    })
  },
  //收藏
  hand () {
    var that = this;
    let iscold = !this.data.iscold
    wx.request({
      url: app.globalData.api +'favor/',
      data: {
        // id
        favorites: that.data.department_id,
      },
      method: 'post', 
      header: { cookie: app.globalData.header },
      success: function(res){
        that.setData({
          iscold
        })
        wx.showToast({
          title: iscold ? '收藏成功' : '取消收藏',
          icon: 'success'
        })
      },
    })
   
  },
  //分享
  fenxiang () {
    wx.showShareMenu({
      withShareTicket: true,
      menus: ['shareAppMessage', 'shareTimeline']
    })
  },
  // 提交添加人员信息
  formSubmit: function (e) {
    var that = this;
    let { phone, suerName, sex } = e.detail.value;
    if (!phone || !suerName) {
      this.setData({
        warn: "就诊人姓名或手机号不能为空！",
        isSubmit: true
      })
      return;
    }
    this.setData({
      phone,
      suerName,
      sex
    })
    wx.request({
      url: app.globalData.api + 'reserve/',
      data: {
        doctor_id: that.data.id,
        retime: that.data.dates,
        name: that.data.suerName,
        phone: that.data.phone
      },
      method: 'post', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      header: { cookie: app.globalData.header },
      success: function (res) {
        wx.showToast({
          title: '预约成功！',
        }),
        that.setData({
          suerName:'',
          phone:''
        })
        //跳转个人中心挂号记录
        wx.navigateTo({
          url: '../registration/registration',
        })
      },
    })

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
